package com.ygg.controller.api;

import com.ygg.model.user.UserInfo;
import com.ygg.result.Result;
import com.ygg.service.UserInfoService;
import com.ygg.utils.AuthContextHolder;
import com.ygg.utils.IpUtil;
import com.ygg.vo.user.LoginVo;
import com.ygg.vo.user.UserAuthVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping("/api/user")
public class UserInfoController {

    @Resource
    UserInfoService userInfoService;

    @ApiOperation(value = "会员登录")
    @PostMapping("login")
    public Result<?> login(@RequestBody LoginVo loginVo, HttpServletRequest request) {
        loginVo.setIp(IpUtil.getIpAddr(request));
        Map<String, Object> info = userInfoService.login(loginVo);
        return Result.ok(info);
    }

    @PostMapping("auth/userAuth")
    public Result userAuth(@RequestBody UserAuthVo userAuthVo, HttpServletRequest request) {

        userInfoService.userAuth(userAuthVo, AuthContextHolder.getUserId(request));
        return Result.ok();
    }


    @GetMapping("auth/getInfo")
    public Result getById(HttpServletRequest request) {
        UserInfo userInfo = userInfoService.show(AuthContextHolder.getUserId(request));
        return Result.ok(userInfo);
    }


}
